package com.example.wnn.web.controller;

import com.example.wnn.config.aop.MyAnnotation;
import com.example.wnn.utils.IpUtils;
import com.example.wnn.web.vo.PartETag;
import com.example.wnn.web.vo.Pson;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/api/hello")
@MyAnnotation
public class HelloWord {

    @Autowired
    private HttpServletRequest request;

    @CrossOrigin(value = "http://localhost:8080")  //后端解决跨域问题
    @GetMapping
    public String hello() throws UnknownHostException {

        log.info("hello woird ----->info");
        log.warn("hello woird ----->warn");
        log.error("hello woird ----->error");
        log.trace("hello woird ----->trace");

        InetAddress addr = InetAddress.getLocalHost();
        System.out.println(addr);
        System.out.println(addr.getHostAddress());
        System.out.println(addr.getHostName());

        System.out.println(IpUtils.findIpFromRequest(request));

        return "hello woird, wnn";
    }

    @PostMapping
    public String p1(@RequestBody Pson pson) throws UnknownHostException {

         List<PartETag> partETagList = pson.getPartETags();

        List<PartETag> newPartETags = pson.getNewPartETags();

        for (PartETag newPartETag : newPartETags) {
                int partNumber = newPartETag.getPartNumber();
                String md5 = newPartETag.getETag();

            Optional<PartETag> first = partETagList.stream().
                    filter(item -> item.getPartNumber() == partNumber).findFirst();
            if (first.isPresent()){
                if (!first.get().getETag().equals(md5)){
                    System.out.println(first.get().getPartNumber());
                }
            }
        }

        return "hello woird, wnn";
    }



}
